/**
 * 
 * @param {查找第一个值为target的位置} arr 
 * @param {目标对象} target 
 */
function binarySearchLeft(arr, target) {
    let i = 0, j = arr.length - 1;
    while (i <= j) {
        let m = Math.floor((i + j) / 2);
        if(arr[m] === target){
            return m;
        }else if (arr[m] < target) {
            i = m + 1;
        } else {
            j = m-1;
            // console.log(m);
        }
    }
    return -1;
}
console.log(binarySearchLeft([1, 2, 3, 4, 5, 5, 5, 6, 7], 5));

/**
 * 查找到右边界
 */
function getRightBound(arr, target) {
    let left = 0;
    let right = arr.length - 1;
    while (left <= right) {
        let mid = left + Math.floor((right - left) >> 1)
        if (arr[mid] <= target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return right;
}

/**
 * 到最后一个比target小的
 */
function getLastMin(arr,target){
    let left = 0;
    let right = arr.length - 1;
    while(left <= right){
        let mid = left+Math.floor((right-left)>>1);
        if(arr[mid] < target){
            if(mid === 0 || arr[mid+1] >= target){
                return mid;
            }else{
                left = mid+1;
            }
        }else{
            right = mid-1;
        }
    }
    return left;
}

let arr = [1, 2, 4, 5, 6, 6, 6, 6, 6, 9];
console.log(getRightBound(arr, 6));

console.log(getLastMin(arr,7))